package rexsee.core.storage;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class RexseePreference implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Preference";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseePreference(childBrowser);
	}

	private final String DEFAULT_PREFERENCE_NAME = "defaultRexseePreference";
	private final Context mContext;
	private final Browser mBrowser;

	public RexseePreference(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
	}

	protected boolean isValidPreferenceName(String preferenceName) {
		return true;
	}

	//JavaScript Interface
	public boolean exists(String key) {
		return exists(key, DEFAULT_PREFERENCE_NAME);
	}
	public boolean exists(String key, String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return false;
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		return (preference == null) ? false : preference.contains(key);
	}
	public void set(String key, String value) {
		set(key, value, DEFAULT_PREFERENCE_NAME);
	}
	public void setItem(String key, String value) {
		set(key, value);
	}
	public void set(String key, String value, String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return;
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.putString(key, value);
		editor.commit();
	}
	public String get(String key) {
		return get(key, DEFAULT_PREFERENCE_NAME);
	}
	public String getItem(String key) {
		return get(key);
	}
	public String get(String key, String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return "";
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		return (preference == null) ? "" : preference.getString(key, "");
	}
	public String getKeys() {
		return getKeys(DEFAULT_PREFERENCE_NAME);
	}
	public String getKeys(String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return "[]";
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		if (preference == null) return "";
		Object[] arr = preference.getAll().keySet().toArray();
		String rtn = "";
		for (int i = 0; i < arr.length; i++) {
			rtn += (i == 0) ? "\"" + arr[i].toString() + "\"" : ",\"" + arr[i].toString() + "\"";
		}
		return "[" + rtn + "]";
	}
	public void remove(String key) {
		remove(key, DEFAULT_PREFERENCE_NAME);
	}
	public void removeItem(String key) {
		remove(key);
	}
	public void remove(String key, String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return;
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.remove(key);
		editor.commit();
	}
	public void clear() {
		clear(DEFAULT_PREFERENCE_NAME);
	}
	public void clear(String preferenceName) {
		if (!isValidPreferenceName(preferenceName)) {
			mBrowser.exception(getInterfaceName(), "Invalid preference name.");
			return;
		}
		SharedPreferences preference = mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
		Editor editor = preference.edit();
		editor.clear();
		editor.commit();
	}

}
